﻿Option Infer On
#Region "NameSpace"
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Net.Mail
Imports System.Net
Imports System.Text
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Imports System.Linq
Imports System.Xml.Linq
Imports System.Linq.Expressions
Imports Crystal.DatabaseManagement.BusinessLayer
#End Region
Namespace BusinessLayer
    Public Class PublicMethods
#Region "DECLARE"
        Private m_UserName As String
        Private m_Password As String
#End Region
#Region "Properties"
        Public Property NewUserName() As String
            Get
                Return m_UserName
            End Get
            Set(ByVal value As String)
                m_UserName = value
            End Set
        End Property
        Public Property NewPassWord() As String
            Get
                Return m_Password
            End Get
            Set(ByVal value As String)
                m_Password = value
            End Set
        End Property

#End Region
#Region "Transaction"
        ''' <summary>
        ''' To check Login Username and Password.
        ''' </summary>
        ''' <param name="UserName"></param>
        ''' <param name="_Password"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>

        Public Shared Function ChkLogin(ByVal UserName As String, ByVal _Password As String) As DataTable
            'Const SEL_TestProcedure = "Cp_Sp_CHK_LoginDetails"
            Dim dsResult As DataSet = Nothing
            'Try

            dsResult = CreateDataBase().ExecuteDataSet("Cp_Sp_CHK_LoginDetails", New Object() {UserName, _Password})
            If dsResult.Tables.Count > 0 Then
                Return dsResult.Tables(0)
            Else
                Return Nothing
            End If

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
        ''' <summary>
        ''' Getting Tasks by Projects 
        ''' </summary>
        ''' <param name="_ProjectId"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetProjectTasks(ByVal _ProjectId As Integer) As DataTable
            ' Const SEL_TestProcedure = "Cp_Sp_GetTasksByProjectId"
            Dim dsResult As DataSet = Nothing
            'Try
            dsResult = CreateDataBase().ExecuteDataSet("Cp_Sp_GetTasksByProjectId", New Object() {_ProjectId})
            If dsResult.Tables.Count > 0 Then
                Return dsResult.Tables(0)
            Else
                Return Nothing
            End If

            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
        ''' <summary>
        ''' Changing User Login Details
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ChangeLoginDetails() As Integer
            Dim objBase As New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            Dim _iResult As Integer
            'Try
            Dim updateUserLogin = (From P In objBase.GetTable(Of CP_USM_User)() _
                                                 Where P.UserID = UserId _
                                                  Select P).SingleOrDefault()
            With updateUserLogin
                .UserName = NewUserName
                .Password = NewPassWord
                objBase.SubmitChanges()
                _iResult = .UserID
            End With
            Return _iResult
            'Catch ex As Exception
            '    Throw ex
            'End Try
            
        End Function

        ''' <summary>
        ''' Is User Name already exists
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function IsUserNameExists() As Boolean
            Dim objBase As New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            Dim query = From USR In objBase.CP_USM_Users _
                                     Where USR.UserName = NewUserName And USR.UserID <> UserId
            If query Is Nothing Or query.Count = 0 Then
                Return False
            Else
                Return True
            End If
        End Function
        ''' <summary>
        ''' Deleting the selected task
        ''' </summary>
        ''' <param name="_TaskId"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function DelTaskDetails(ByVal _TaskId As Integer) As Integer
            Dim objBase As BaseClassDataContext
            ' Try
            objBase = New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            Dim UpdatedTask = (From u In objBase.Cp_Prj_UpdatedTasks _
                          Where u.TaskId = _TaskId _
                          Select u).ToList()
            objBase.Cp_Prj_UpdatedTasks.DeleteAllOnSubmit(UpdatedTask)
            objBase.SubmitChanges()
            Dim Tasks = (From t In objBase.CP_PRJ_Tasks _
                          Where t.TaskID = _TaskId _
                          Select t).ToList()
            objBase.CP_PRJ_Tasks.DeleteAllOnSubmit(Tasks)
            objBase.SubmitChanges()
            Return 0
            'Catch ex As Exception
            '    Throw ex
            '    Return -1
            'End Try

        End Function
        Public Shared Sub UpdateLoginUserDetails(ByVal isLogged As Boolean)
            Dim objBase As New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            objBase.CommandTimeout = 0
            ' Try
            Dim query = (From u In objBase.GetTable(Of CP_USM_User)() _
                                    Where u.UserID = UserId _
                                    Select u).SingleOrDefault
            If isLogged Then
                query.IsLogged = True
            Else
                query.IsLogged = False
            End If
            objBase.SubmitChanges()
            'Catch ex As Exception
            '    Throw ex
            'End Try


        End Sub
        Public Shared Function blnIsUserLoggedAlready() As Boolean
            ' Try
            Dim objBase As New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            Dim query = (From u In objBase.CP_USM_Users _
                        Where u.UserID = UserId _
                        Select u.IsLogged).SingleOrDefault
            Return query
            'Catch ex As Exception
            '    Throw ex
            'End Try

        End Function
        Public Shared Sub UpdateLoggedUserDetails(ByVal isLogged As Boolean, ByVal projectId As Integer)
            Dim objBase As New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            Dim Combined As New Cp_Combined

            ' Try
            Dim query = (From u In objBase.Cp_Combineds _
                         Where u.ProjectID = projectId And u.UserId = UserId _
                         Select u).SingleOrDefault
            Dim isExists = (From u In objBase.Cp_Combineds _
                              Where u.ProjectID = projectId And u.UserId = UserId _
                              Select u).Count
            If isLogged Then

                If isExists = 0 Then
                    Combined.UserId = UserId
                    Combined.ProjectID = projectId
                    objBase.Cp_Combineds.InsertOnSubmit(Combined)
                End If

            Else
                If projectId > 0 Then
                    If isExists > 0 Then
                        objBase.Cp_Combineds.DeleteOnSubmit(query)
                    End If
                Else
                    Dim query1 = (From u In objBase.Cp_Combineds _
                                 Where u.UserId = UserId _
                                  Select u).ToList()
                    If query1.Count > 1 Then
                        objBase.Cp_Combineds.DeleteAllOnSubmit(query1)
                    End If
                End If

            End If
            ' End If
            objBase.SubmitChanges()
            'Catch ex As Exception
            '    Throw ex
            'End Try


        End Sub
        Public Shared Function blnIsUserAccessingCombining(ByVal projectId As Integer) As DataTable
            'Try
            Dim objBase As New BaseClassDataContext(BLLmdlCommon.strConfiguration)
            'Dim _UserId = (From u In objBase.Cp_Combineds _
            '               Where u.Project = 1 _
            '               Select u.UserId).SingleOrDefault
            'If _UserId <> UserId Then
            Dim ds As New DataSet()
            Dim XElement As XElement
            XElement = New XElement("GetLoginUserDetails", _
                                             From u In objBase.Cp_Combineds _
                                             Join usr In objBase.CP_USM_Users On u.UserId Equals usr.UserID _
                                             Where u.ProjectID = projectId _
                                             Select New XElement("List", _
                                                                 New XElement("UserId", u.UserId), _
                                                                 New XElement("UserName", usr.FirstName + " " + usr.LastName)))
            ds.ReadXml(New System.IO.StringReader(XElement.ToString()))
            If ds.Tables.Count > 0 Then
                Return ds.Tables(0)
            Else
                Return Nothing
            End If
            ' End If
            'Catch ex As Exception
            '    Throw ex
            'End Try

        End Function
        Public Shared Function GetProjectStatus(ByVal SelectedProjectId As Integer) As Integer
            Dim _ProgressStatus As Integer
            'Const SEL_TestProcedure = "Cp_SP_Prj_ProgressBarStatus"
            ' Try
            _ProgressStatus = CreateDataBase().ExecuteScalar("Cp_SP_Prj_ProgressBarStatus", New Object() {SelectedProjectId})
            Return _ProgressStatus
            'Catch ex As Exception
            '    Throw ex
            'End Try
        End Function
       
#End Region
    End Class
End Namespace

